package com.toweave.starter;

import static com.toweave.starter.utils.ConstantsUtil.LIZ_TAG;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.window.layout.WindowInfoTracker;
import androidx.window.layout.WindowMetrics;
import androidx.window.layout.WindowMetricsCalculator;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.toweave.starter.activity.AlertDialogActivity;
import com.toweave.starter.activity.AnimatorTestActivity;
import com.toweave.starter.activity.BaseAdapterActivity;
import com.toweave.starter.activity.BindingTestActivity;
import com.toweave.starter.activity.CreateOriginalActivity;
import com.toweave.starter.activity.DynamicFragmentActivity;
import com.toweave.starter.activity.EditViewActivity;
import com.toweave.starter.activity.ListViewActivity;
import com.toweave.starter.activity.PieChartActivity;
import com.toweave.starter.activity.RequestActivity;
import com.toweave.starter.activity.SpinnerDialogActivity;
import com.toweave.starter.activity.SpinnerDropdownActivity;
import com.toweave.starter.activity.StaticFragmentActivity;
import com.toweave.starter.activity.TextViewActivity;
import com.toweave.starter.components.ToastUtil;
import com.toweave.starter.utils.SystemUtil;

import java.util.Locale;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  private ViewGroup container;
  private WindowInfoTracker windowInfoTracker;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 使用findViewById()而不是View Binding
    container = findViewById(R.id.container);
    windowInfoTracker = WindowInfoTracker.getOrCreate(this);

    // Add a utility view to the container to hook into View.onConfigurationChanged().
    container.addView(new View(this) {
      @Override
      protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        computeWindowSizeClasses();
      }
    });

    computeWindowSizeClasses();

    TextView btn_goto_request_activity = findViewById(R.id.btn_goto_request_activity);
    btn_goto_request_activity.setOnClickListener(this);

    TextView btn_goto_edit_view_activity = findViewById(R.id.btn_goto_edit_view_activity);
    btn_goto_edit_view_activity.setOnClickListener(this);

    TextView btn_goto_alert_dialog_activity = findViewById(R.id.btn_goto_alert_dialog_activity);
    btn_goto_alert_dialog_activity.setOnClickListener(this);

    TextView btn_device_wifi_info = findViewById(R.id.btn_device_wifi_info);
    btn_device_wifi_info.setOnClickListener(this);

    TextView btn_dropdown_select = findViewById(R.id.btn_dropdown_select);
    btn_dropdown_select.setOnClickListener(this);

    TextView btn_dialog_select = findViewById(R.id.btn_dialog_select);
    btn_dialog_select.setOnClickListener(this);

    TextView btn_base_adapter = findViewById(R.id.btn_base_adapter);
    btn_base_adapter.setOnClickListener(this);

    TextView btn_list_view = findViewById(R.id.btn_list_view);
    btn_list_view.setOnClickListener(this);

    TextView btn_pie_chart = findViewById(R.id.btn_pie_chart);
    btn_pie_chart.setBackgroundColor(Color.BLACK);
    btn_pie_chart.setOnClickListener(this);

    TextView btn_static_fragment_activity = findViewById(R.id.btn_static_fragment_activity);
    btn_static_fragment_activity.setOnClickListener(this);

    TextView btn_goto_text_view_activity = findViewById(R.id.btn_goto_text_view_activity);
    btn_goto_text_view_activity.setOnClickListener(this);

    TextView btn_goto_animator_test_activity = findViewById(R.id.btn_goto_animator_test_activity);
    btn_goto_animator_test_activity.setOnClickListener(this);

    TextView btn_goto_create_original_activity = findViewById(R.id.btn_goto_create_original_activity);
    btn_goto_create_original_activity.setOnClickListener(this);

    TextView btn_goto_dynamic_fragment_activity = findViewById(R.id.btn_goto_dynamic_fragment_activity);
    btn_goto_dynamic_fragment_activity.setOnClickListener(this);

    TextView btn_goto_binding_test_activity = findViewById(R.id.btn_goto_binding_test_activity);
    btn_goto_binding_test_activity.setOnClickListener(this);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setTooltipText("Send an email 123");
  }

  @Override
  public void onClick(View v) {
    Log.d("-liz-", "onClick: btn_goto_request_activity");
    Intent intent = new Intent(MainActivity.this, RequestActivity.class);
    int id = v.getId();
    if (id == R.id.btn_goto_request_activity) {
      Log.d("-liz-", "onClick: btn_goto_request_activity");
    } else if (id == R.id.btn_goto_edit_view_activity) {
      intent = new Intent(MainActivity.this, EditViewActivity.class);
      Log.d("-liz-", "onClick: btn_goto_request_activity");
    } else if (id == R.id.btn_goto_alert_dialog_activity) {
      intent = new Intent(MainActivity.this, AlertDialogActivity.class);
      Log.d("-liz-", "onClick: btn_goto_request_activity");
    } else if (id == R.id.btn_device_wifi_info) {
      Log.d("-liz-", "onClick: btn_device_wifi_info");
      String macAddressFull = SystemUtil.getMacAddressFull(MainActivity.this);
      new ToastUtil(MainActivity.this, macAddressFull);
      return;
    } else if (id == R.id.btn_dropdown_select) {
      Log.d("-liz-", "onClick: btn_dropdown_select");
      intent = new Intent(MainActivity.this, SpinnerDropdownActivity.class);
    } else if (id == R.id.btn_dialog_select) {
      Log.d("-liz-", "onClick: btn_dialog_select");
      intent = new Intent(MainActivity.this, SpinnerDialogActivity.class);
    } else if (id == R.id.btn_base_adapter) {
      Log.d("-liz-", "onClick: btn_base_adapter");
      intent = new Intent(MainActivity.this, BaseAdapterActivity.class);
    } else if (id == R.id.btn_list_view) {
      Log.d("-liz-", "onClick: btn_list_view");
      intent = new Intent(MainActivity.this, ListViewActivity.class);
    } else if (id == R.id.btn_pie_chart) {
      Log.d("-liz-", "onClick: btn_list_view");
      intent = new Intent(MainActivity.this, PieChartActivity.class);
    } else if (id == R.id.btn_static_fragment_activity) {
      Log.d("-liz-", "onClick: btn_static_fragment_activity");
      intent = new Intent(MainActivity.this, StaticFragmentActivity.class);
    } else if (id == R.id.btn_goto_text_view_activity) {
      Log.d("-liz-", "onClick: btn_goto_text_view_activity");
      intent = new Intent(MainActivity.this, TextViewActivity.class);
    } else if (id == R.id.btn_goto_animator_test_activity) {
      Log.d("-liz-", "onClick: btn_goto_animator_test_activity");
      intent = new Intent(MainActivity.this, AnimatorTestActivity.class);
    } else if (id == R.id.btn_goto_create_original_activity) {
      Log.d("-liz-", "onClick: btn_goto_create_original_activity");
      intent = new Intent(MainActivity.this, CreateOriginalActivity.class);
    } else if (id == R.id.btn_goto_dynamic_fragment_activity) {
      Log.d("-liz-", "onClick: btn_goto_dynamic_fragment_activity");
      intent = new Intent(MainActivity.this, DynamicFragmentActivity.class);
    } else if (id == R.id.btn_goto_binding_test_activity) {
      Log.d("-liz-", "onClick: btn_goto_binding_test_activity");
      intent = new Intent(MainActivity.this, BindingTestActivity.class);
    }
    //   switch (v.getId()) {
    //   case R.id.btn_goto_request_activity:
    //     Log.d("-liz-", "onClick: btn_goto_request_activity");
    //     break;
    //   case R.id.btn_goto_edit_view_activity:
    //     intent = new Intent(MainActivity.this, EditViewActivity.class);
    //     Log.d("-liz-", "onClick: btn_goto_request_activity");
    //     break;
    // }
    startActivity(intent);
  }


  private void computeWindowSizeClasses() {
    // Alternatively, you can get the current WindowMetrics using WindowMetricsCalculator
    WindowMetrics metrics = WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(this);
    // Use metrics to get information about the current window size, etc.
    int width = metrics.getBounds().width();
    int height = metrics.getBounds().height();
    float density = getResources().getDisplayMetrics().density;
    String str = String.format(Locale.CHINA, "density: %d, density: %d density: %f", width, height, density);
    Log.i(LIZ_TAG, str);
    // Use widthWindowSizeClass and heightWindowSizeClass.
  }

  // end
}